package com.moli.iov.notify.util;

import com.aliyuncs.exceptions.ClientException;
import com.moli.iov.model.SMSResponse;
import com.moli.iov.notify.config.SmsProperties;
import com.moli.iov.notify.model.dto.SmsDto;
import com.moli.iov.util.SMSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *
 * </p>
 *
 * @author zql
 * @since 2019/9/10 16:22
 */
@Slf4j
@Component
public class SendSmsUtil {

  @Autowired
  private SmsProperties smsProperties;

  /**
   * 发送短信
   * @param smsDto
   */
  public boolean sendSms(SmsDto smsDto){
    try {
      String accessKeyId = smsProperties.getAccessKeyId();
      String secret = smsProperties.getAccessSecret();
      String sign = smsProperties.getSign();

      SMSResponse  smsSResponse = SMSUtil.sendMessage(accessKeyId, secret, smsDto.getMobile(),
          smsDto.getTempleteCode(), sign, smsDto.getJsonData());

      if (!"OK".equals(smsSResponse.getCode())) {

        log.error("发送短信失败，数据：{}, 失败信息：{},请求ID:{},回执ID:{}",
            smsDto, smsSResponse.getMessage(), smsSResponse.getRequestId(), smsSResponse.getBizId());

        return false;
      }
      log.info("发送短信成功");
      return true;
    } catch (ClientException e) {
      log.error("发送短信异常", e);
      return false;
    }
  }
}
